import java.util.Arrays;

public class EraseOverlapIntervals {

    public int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> {
            if (a[0] != b[0]) return a[0] - b[0];
            return a[1] - b[1];
        });
        int end = Integer.MIN_VALUE;
        int ans = 0;
        for (int[] interval : intervals) {
            if (interval[0] >= end) {
                end = interval[1];
            } else {
                if (interval[1] < end) end = interval[1];
                ++ans;
            }
        }
        return ans;
    }
}
